package gorm_plugin

import (
	"gitee.com/kristas/booting-go/framework/core/statement/types"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
)

type NamingStrategy struct {
	TablePrefix   string `yaml:"table_prefix"`
	SingularTable bool   `yaml:"singular_table"`
}

type ConfigAdapter struct {
	types.AutoConfigure                      `prefix:"gorm"`
	Level                                    string          `yaml:"loglevel"`
	SkipDefaultTransaction                   bool            `yaml:"skip_default_transaction"`
	NamingStrategy                           *NamingStrategy `yaml:"naming_strategy"`
	FullSaveAssociations                     bool            `yaml:"full_save_associations"`
	DryRun                                   bool            `yaml:"dry_run"`
	PrepareStmt                              bool            `yaml:"prepare_stmt"`
	DisableAutomaticPing                     bool            `yaml:"disable_automatic_ping"`
	DisableForeignKeyConstraintWhenMigrating bool            `yaml:"disable_foreign_key_constraint_when_migrating"`
	DisableNestedTransaction                 bool            `yaml:"disable_nested_transaction"`
	AllowGlobalUpdate                        bool            `yaml:"allow_global_update"`
	QueryFields                              bool            `yaml:"query_fields"`
	CreateBatchSize                          int             `yaml:"create_batch_size"`
}

func (g *ConfigAdapter) ToGormConfig() *gorm.Config {
	return &gorm.Config{
		SkipDefaultTransaction: g.SkipDefaultTransaction,
		NamingStrategy: schema.NamingStrategy{
			TablePrefix:   "",
			SingularTable: g.NamingStrategy.SingularTable,
		},
		FullSaveAssociations:                     g.FullSaveAssociations,
		DryRun:                                   g.DryRun,
		PrepareStmt:                              g.PrepareStmt,
		DisableAutomaticPing:                     g.DisableAutomaticPing,
		DisableForeignKeyConstraintWhenMigrating: g.DisableForeignKeyConstraintWhenMigrating,
		DisableNestedTransaction:                 g.DisableNestedTransaction,
		AllowGlobalUpdate:                        g.AllowGlobalUpdate,
		QueryFields:                              g.QueryFields,
		CreateBatchSize:                          g.CreateBatchSize,
	}
}
